import axios from 'axios';
import { _getLunBoData ,_getAuthorData,
    _getTopicData,_getArticleData} from '../../common/api/index.js'

const getHomeContent = (result) => ({
    type:'GET_HOME_DATA_INFO',
    homeArticles:result.homeArticles,
    homeTopicImages:result.homeTopicImages,
    homeLunBoImages:result.homeLunBoImages,
    authorList:result.authorList,
    } 
);

const lunBoImage=(data) =>({
    type:'GET_LUNBO_IMAGE',
    data,
});

const author=(data) =>({
    type:'GET_AUTHOR_DATA',
    data,
});
const topice=(data) =>({
    type:"GET_TOPICE_DATA",
    data
})
const article =(data,index,hasNextPage)=>({
    type:"GET_ARTICLE_DATA",
    data,
    index,
    hasNextPage
})

export const getHomeInfo = () => {
    return (dispacth) => {
        axios.get('/api/getHomeContentData.json').then((res) =>{
            const  result = res.data.data;        
            dispacth(getHomeContent(result))
            })
        .catch(() => {
            console.log('404  接口请求失败，请重试！！')
        })  
    }    
};
export const getArticleData= (index,hasNextPage) =>{
    return(dispacth) =>{
        async function handleRequest(){
            let data = await _getArticleData(index)
            if(data.status!==200){
                return
            }else{
                dispacth(article(data.data,hasNextPage,index))
            }
        }
        handleRequest()
    }
};

export const getTopicData =() =>{
    return(dispacth) =>{
        async function handleRequest(){
            let data =await _getTopicData();
            if(data.status!==200){
                return
            }else{
                dispacth(topice(data.data))
            }
        }
    handleRequest();
    }
}

export const gitAuthors =() =>{
    return (dispatch) =>{
        async function handleRequest(){
           let data = await _getAuthorData();
           if(data.status!==200 ){
                return
           }else{
                dispatch(author(data.data))
           }
        }
        handleRequest()
    }  
};

export const gitLunBoImages =(pagenum,index) =>{
    return(dispatch) => {
        async function handleRequestlunData(){
            let data = await _getLunBoData(pagenum,index);
            if(data.status!==200){
                return 
            }else{
                dispatch(lunBoImage(data.data))
            }
        }
        handleRequestlunData()   
    }
}
